package org.jcnc.IDEology.kernel.module;

import org.jcnc.IDEology.kernel.Module;
import org.jcnc.IDEology.kernel.kernel.Kernel;

import java.util.HashMap;
import java.util.Map;

/**
 * Communication module, responsible for managing communication between modules
 */
public class CommunicationModule extends KernelModule implements Module{
    private final Kernel kernel;
    private final Map<String, Module> communicationPartners;

    public CommunicationModule(Kernel kernel) {
        this.kernel = kernel;
        this.communicationPartners = new HashMap<>();
    }

    /**
     * Adds a communication partner
     * @param name The name of the partner
     * @param module The module of the partner
     */
    public void addCommunicationPartner(String name, Module module) {
        this.communicationPartners.put(name, module);
    }

    /**
     * Sends a message to a communication partner
     * @param name The name of the partner
     * @param message The message to be sent
     */
    public void sendMessage(String name, String message) {
        Module partner = this.communicationPartners.get(name);
        if (partner != null) {
            partner.receiveMessage(message);
        } else {
            System.out.println("Communication partner " + name + " does not exist.");
        }
    }
}
